package com.maszy.commons.tools;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocFlavor.SERVICE_FORMATTED;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.swing.JOptionPane;


import com.maszy.bean.GoodPlan;
import com.maszy.bean.Product;
import com.maszy.service.GoodPlanService;



public class PrintToolTest implements Printable{
	   private int PAGES = 0;
	   /*
	   * Graphic指明打印的图形环境；PageFormat指明打印页格式（页面大小以点为计量单位，
	   * 1点为1英寸的1/72，1英寸为25.4毫米。A4纸大致为595×842点）；page指明页号
	   */
	   private final int _titleFontSize=15;
	   private final int _contentFontSize=12;
	   private final int _x=20;
	   private final int _x2=300;
	   private final int _y=20;
	   private final int step=20;
	   String goodType="";
	   private GoodPlan goodplan;
	   public PrintToolTest(GoodPlan good){
		   this.goodplan=good;
	   }
	   public PrintToolTest(){
		
	   }
	   public GoodPlan getGoodplan() {
		return goodplan;
	}
	public void setGoodplan(GoodPlan goodplan) {
		this.goodplan = goodplan;
	}


	   private void printFileAction() 
	   { 
	       // 构造一个文件选择器，默认为当前目录
	            File file = new   File("c:\\123.html");// 获取选择的文件
	            // 构建打印请求属性集
	           PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
	            // 设置打印格式，因为未确定文件类型，这里选择 AUTOSENSE 
	           DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; 
	            // 查找所有的可用打印服务
	            // 定位默认的打印服务
	           PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
	           // 显示打印对话框
	                try 
	               { 
	                   DocPrintJob job = defaultService.createPrintJob();// 创建打印作业
	                   FileInputStream fis = new FileInputStream(file);// 构造待打印的文件流
	                   DocAttributeSet das = new HashDocAttributeSet(); 
	                   Doc doc = new SimpleDoc(fis, flavor, das);// 建立打印文件格式
	                   job.print(doc, pras);// 进行文件的打印
	               } 
	               catch(Exception e) 
	               { 
	                    e.printStackTrace(); 
	               } 
	   }
	   private void printUrlImage() 
	   { 
	       // 构造一个文件选择器，默认为当前目录
	            File file = new   File("c:\\123.html");// 获取选择的文件
	            // 构建打印请求属性集
	           PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
	            // 设置打印格式，因为未确定文件类型，这里选择 AUTOSENSE 
	           DocFlavor flavor = DocFlavor.URL.TEXT_HTML_UTF_8; 
	           
	            // 查找所有的可用打印服务
	            // 定位默认的打印服务
	           PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
	           for(int i=0;i<defaultService.getSupportedDocFlavors().length;i++){
//	        	   System.out.println(defaultService.getSupportedDocFlavors()[i]);
	           }
	           // 显示打印对话框
	                try 
	               { 
	                   URL url=new URL("http://wenku.baidu.com/view/2175d02b915f804d2b16c1be.html");
	                   DocPrintJob job = defaultService.createPrintJob();// 创建打印作业
	                   FileInputStream fis = new FileInputStream(file);// 构造待打印的文件流
	                   DocAttributeSet das = new HashDocAttributeSet(); 
	                   Doc doc = new SimpleDoc(url, flavor, das);// 建立打印文件格式
	                   job.print(doc, pras);// 进行文件的打印
	               } 
	               catch(Exception e) 
	               { 
	                    e.printStackTrace(); 
	               } 
	   }
	public static void main(String args[]) throws FileNotFoundException, MalformedURLException{
		PrintToolTest ptool=new PrintToolTest();
		ptool.printUrlImage();
	}
	/* (non-Javadoc)
	 * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
	 */
	 public int print(Graphics g, PageFormat pf, int page) throws PrinterException 
	 { 
		 	System.out.println("!!!!!!!!");
	    Graphics2D g2 = (Graphics2D)g; 
	    g2.setPaint(Color.black); // 设置打印颜色为黑色
		 if (page >= PAGES) // 当打印页号大于需要打印的总页数时，打印工作结束
				 return Printable.NO_SUCH_PAGE; 
		    g2.translate(pf.getImageableX(), pf.getImageableY());// 转换坐标，确定打印边界
		    g2.drawString("xxxxxxxxxxxxxxxxxxxx", 100, 100);
		 return Printable.PAGE_EXISTS; // 存在打印页时，继续打印工作
		 } 
		 /* 打印指定页号的具体文本内容 */ 
	 
		
}
